<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Member extends CI_Controller {
	var $tablefunc = 'member';
	var $fields = array('mname','mgroup','email','mnick','sex','phone','ischeck','isdisable','iscomm','isleader');
	var $funcarr = array('add','check','uncheck','disable','enable','del');
	function __construct(){
		parent::__construct();
		$this->Lang_model->loadLang('admin');
		$this->load->helper('array');
		$this->load->model('Purview_model');
		$this->Data_model->setTable($this->tablefunc);
	}
	
	public function index(){
		$this->Purview_model->checkPurview($this->tablefunc);
		$post = $this->input->post(NULL,TRUE);
		$getwhere = array();
		$search['mgroup'] = trim($post['mgroup']);
		$search['keyword'] = trim($post['keyword']);
		$search['searchtype'] = trim($post['searchtype']);
		if($search['mgroup']>0){
			$getwhere['mgroup']=$search['mgroup'];
		}
		if($search['searchtype']=='id'){
			if($search['keyword']!=''){
				$getwhere[$search['searchtype']]=$search['keyword'];
			}
		}else{
			if($search['keyword']!=''){
				$getwhere[$search['searchtype'].' like']='%'.$search['keyword'].'%';
			}
		}
		$pagearr=array(
			'currentpage'=>	isset($post['currentpage'])&&$post['currentpage']>0?$post['currentpage']:1,
			'totalnum'=>$this->Data_model->getDataNum($getwhere),
			'pagenum'=>20
		);
		
		$data = $this->Data_model->getData($getwhere,'mid desc',$pagearr['pagenum'],($pagearr['currentpage']-1)*$pagearr['pagenum']);

		$res = array(
				'tpl'=>'list',
				'tablefunc'=>$this->tablefunc,
				'search'=>$search,
				'membergroup'=>$this->Data_model->getData('','listorder',0,0,'membergroup'),
				'liststr'=>$this->_setlist($data,true),
				'pagestr'=>show_page($pagearr,$search),
				'funcstr'=>$this->Purview_model->getFunc($this->tablefunc,$this->funcarr),
		);
		$this->load->view($this->tablefunc,$res);
	}
	
	public function add(){
		$this->Purview_model->checkPurviewAjax($this->tablefunc,'add');
		$post = $this->input->post(NULL,TRUE);
		if($post['action']==site_aurl($this->tablefunc)){
			if($this->Data_model->getSingle(array('username'=>$post['username']))){
				show_jsonmsg(array('status'=>206));
			}
			$time = time();
			$userip = $this->input->ip_address();
			$this->load->helper('string');
			$salt = random_string('alnum',6);
			$data = elements($this->fields,$post);
			$data['createtime'] = $time;
			$data['updatetime'] = $time;
			$lang['lasttime'] = $time;
			$data['regip'] = $userip;
			$data['lastip'] = $userip;
			$data['mrnd'] = $salt;
			$data['mpwd'] = md5pass($post['password'],$salt);
			$id=$this->Data_model->addData($data);
			show_jsonmsg(array('status'=>200,'remsg'=>$this->_setlist($this->Data_model->getSingle(array('id'=>$id)),false)));
		}else{
			$res = array(
					'tpl'=>'view',
					'tablefunc'=>$this->tablefunc,
					'membergroup'=>$this->Data_model->getData('','listorder',0,0,'membergroup')
			);
			show_jsonmsg(array('status'=>200,'remsg'=>$this->load->view($this->tablefunc,$res,true)));
		}
	}

	public function edit(){
		
		$this->Purview_model->checkPurviewAjax($this->tablefunc,'edit');
		$post = $this->input->post(NULL,TRUE);
		if($post['mid']&&$post['action']==site_aurl($this->tablefunc)){
			if($this->Data_model->getSingle(array('mname'=>$post['mname'],'mid !='=>$post['mid']))){
				show_jsonmsg(array('status'=>206));
			}
			$time = time();
			$data = elements($this->fields,$post);
			$data['lasttime'] = $time;
			if($post['password']!=''){
				$this->load->helper('string');
				$salt = random_string('alnum',6);
				$data['mpwd'] = md5pass($post['password'],$salt);
				$data['mrnd'] = $salt;
			}
			$datawhere = array('mid'=>$post['mid']);
			$this->Data_model->editData($datawhere,$data);
			show_jsonmsg(array('status'=>200,'mid'=>$post['mid'],'remsg'=>$this->_setlist($this->Data_model->getSingle(array('mid'=>$post['mid'])),false)));
		}else{
			$id = $this->uri->segment(4);
			if($id>0 && $view = $this->Data_model->getSingle(array('mid'=>$id))){
				$res = array(
						'tpl'=>'view',
						'tablefunc'=>$this->tablefunc,
						'view'=>$view,
						'membergroup'=>$this->Data_model->getData('','listorder',0,0,'membergroup')
				);
				show_jsonmsg(array('status'=>200,'remsg'=>$this->load->view($this->tablefunc,$res,true)));
			}else{
				show_jsonmsg(array('status'=>203));
			}
		}
	}
	
	public function del(){
		$this->Purview_model->checkPurviewAjax($this->tablefunc,'del');
		$ids = $this->input->post('optid',true);
		if($ids){
			if(	$this->Data_model->delMemberData($ids)){
				show_jsonmsg(array('status'=>200,'remsg'=>lang('delok'),'ids'=>$ids));
			}else{
				how_jsonmsg(array('status'=>207));
			}	
			
		}else{
			show_jsonmsg(array('status'=>203));
		}
	}

	public function check(){
		$this->_setState('check','ischeck',1,'checkok');
	}
	public function uncheck(){
		$this->_setState('check','ischeck',0,'uncheckok');
	}
	public function disable(){
		$this->_setState('disable','isdisable',1,'disableok');
	}
	public function enable(){
		$this->_setState('disable','isdisable',0,'enableok');
	}

	public function login(){
		$this->Purview_model->checkPurviewAjax($this->tablefunc,'login');
		$id=$this->uri->segment(4);
		$member = $this->Data_model->getSingle(array('mid'=>$id));
		if(empty($member)){
			show_jsonmsg(array('status'=>203,'remsg'=>'用户信息错误!'));
		}else{
			$this->load->model('member_model');
			$this->member_model->setLogin($member);
			show_jsonmsg(array('status'=>200,'remsg'=>'登录成功! <a href="'.site_url('member').'" target="_blank">会员中心</a>','hold'=>1));
		}
	}

	function _setState($type,$field,$v,$msg){
		$this->Purview_model->checkPurviewAjax($this->tablefunc,$type);
		$ids = $this->input->post('optid',true);
		if($ids){
			$this->Data_model->editData(array('mid'=>$ids),array($field=>$v),$this->tablefunc);
			show_jsonmsg(array('status'=>200,'remsg'=>lang($msg),'ids'=>$ids));
		}else{
			show_jsonmsg(array('status'=>203));
		}
	}
	
	function _setlist($data,$ismultiple=true){
		$newdata = $ismultiple?$data:($newdata[0]=$data);
		if($ismultiple){
			$newdata = $data;
		}else{
			$newdata = array(0=>$data);
		}
		$newstr = '';
		foreach($newdata as $key=>$item){
			$item['func'] = '';
			if($this->Purview_model->checkPurviewFunc($this->tablefunc,'login')){
				$item['func'] .= $this->Purview_model->getSingleFunc(site_aurl($this->tablefunc.'/login/'.$item['mid']),'login',$item['mid']);
			}
			if($this->Purview_model->checkPurviewFunc($this->tablefunc,'edit')){
				$item['func'] .= $this->Purview_model->getSingleFunc(site_aurl($this->tablefunc.'/edit/'.$item['mid']),'edit');
			}
			if($this->Purview_model->checkPurviewFunc($this->tablefunc,'del')&&$item['mid']>1){
				$item['func'] .=  $this->Purview_model->getSingleFunc(site_aurl($this->tablefunc.'/del/'.$item['mid']),'sdel',$item['mid']);	
			}
			//$disabled = $item['mid']==1?'disabled':'';
			$newstr .='<tr id="tid_'.$item['mid'].'">
			<td width=30><input type=checkbox name="optid[]"  value='.$item['mid'].'></td>
			<td width=40>'.$item['mid'].'</td>
			<td width=80>'.$item['mname'].'</td>
			<td width=120>'.$item['email'].'</td>
			<td width=100>'.$item['mnick'].'</td>
			<td>'.$item['phone'].'</td>
			<td width=120>'.date('Y-m-d H:i:s',$item['regdate']).'</td>
			<td width=120>'.date('Y-m-d H:i:s',$item['lasttime']).'</td>
			<td width=50 >'.
			($item['ischeck']?lang('checked'):lang('nocheck')).
			($item['isdisable']?lang('disabled'):'').
			($item['iscomm']?'已推荐':'').
			($item['isleader']?'领袖':'')
			.'</td>
			<td width=70>'.$item['func'].'</td></tr>';
		}
		return $newstr;
	}
}